/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import DTO.xmlobject.Users.Users;
import DTO.xmlobject.Users.User;

/**
 *
 * @author ASUS
 */
public class UserDAO extends Dataconnection{
    private User ReadUserFromDS(ResultSet rs) {
        User result = new User();
        if (rs != null) {
            try {
                result.setUsername(rs.getString("Username"));
                result.setPassword(rs.getString("Password"));
                result.setFullname(rs.getString("Fullname"));
                result.setEmail(rs.getString("Email"));
                result.setPhone(rs.getString("Phone"));
                result.setAddress(rs.getString("Address"));
                result.setRoleID(rs.getLong("RoleID"));
                result.setIsDelete(rs.getBoolean("IsDelete"));
                result.setCreatedDate(rs.getDate("CreatedDate"));
                result.setCreatedBy(rs.getString("CreatedBy"));
                result.setUpdatedDate(rs.getDate("UpdatedDate"));
                result.setUpdatedBy(rs.getString("UpdatedBy"));
            } catch (SQLException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public User getUserById(int id) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spBDSUserGet(?) }");
            proc_stmt.setLong("UserID", id);
            rs = proc_stmt.executeQuery();
            if (rs.next()) {
                return ReadUserFromDS(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Users getAllUser() {
        Users result = new Users();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spBDSUserGetList() }");
            rs = proc_stmt.executeQuery();
            while (rs.next()) {
                result.getUser().add(ReadUserFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

      public User Create(User value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spBDSUserInsert(?,?,?,?,?,?,?,?,?,?,?,?) }");
            proc_stmt.registerOutParameter("Username", java.sql.Types.INTEGER);
            proc_stmt.setString("Password", value.getPassword());
            proc_stmt.setString("Fullname", value.getFullname());
            proc_stmt.setString("Email", value.getEmail());
            proc_stmt.setString("Phone", value.getPhone());
            proc_stmt.setString("Address", value.getAddress());
            proc_stmt.setLong("RoleID", value.getRoleID());
            proc_stmt.setBoolean("IsDelete", value.isIsDelete());
            proc_stmt.setDate("CreatedDate", value.getCreatedDate());
            proc_stmt.setString("CreatedBy", value.getCreatedBy());
            proc_stmt.setDate("UpdatedDate", value.getUpdatedDate());
            proc_stmt.setString("UpdatedBy", value.getUpdatedBy());
            proc_stmt.execute();
            value.setUsername(proc_stmt.getString("Username"));
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return value;
    }

    public User Edit(User value) {
        try {
          proc_stmt = DAOConnection.prepareCall("{ call spBDSUserDelete(?,?,?,?,?,?,?,?,?,?,?,?) }");
            proc_stmt.registerOutParameter("Username", java.sql.Types.INTEGER);
            proc_stmt.setString("Password", value.getPassword());
            proc_stmt.setString("Fullname", value.getFullname());
            proc_stmt.setString("Email", value.getEmail());
            proc_stmt.setString("Phone", value.getPhone());
            proc_stmt.setString("Address", value.getAddress());
            proc_stmt.setLong("RoleID", value.getRoleID());
            proc_stmt.setBoolean("IsDelete", value.isIsDelete());
            proc_stmt.setDate("CreatedDate", value.getCreatedDate());
            proc_stmt.setString("CreatedBy", value.getCreatedBy());
            proc_stmt.setDate("UpdatedDate", value.getUpdatedDate());
            proc_stmt.setString("UpdatedBy", value.getUpdatedBy());
            proc_stmt.execute();
            return value;
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public boolean Delete(int value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spUserDelete(?) }");
            proc_stmt.setLong("Username", value);
            return proc_stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    public Users Search(String name) {
        Users result = new Users();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spUserSearch(?) }");
            proc_stmt.setString("Username", name);
            rs = proc_stmt.executeQuery();
            while (rs.next()) {
                result.getUser().add(ReadUserFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

}
